//
//  StatisticsCount.m
//  TengyunStation
//
//  Created by Johnny on 15/9/17.
//  Copyright (c) 2015年 xingrong. All rights reserved.
//

#import "StatisticsCount.h"
#import "SocketMethod.h"
#import "Singleton.h"
#import "TSHeader.h"

@implementation StatisticsCount

- (id)initWithData:(NSData *)data {
    self = [super init];
    if (self) {
        int length = 0;
        self.allWithdraw = [[SocketMethod stringSubdata:data Range:NSMakeRange(length, 16)] floatValue];
        length += 16;
        self.frozenMoney = [[SocketMethod stringSubdata:data Range:NSMakeRange(length, 16)] floatValue];
        length += 16;
        self.messageNewTime = [SocketMethod stringSubdata:data Range:NSMakeRange(length, 16)];
        length += 16;
        NSInteger contentLenth = [SocketMethod integerSubdata:data Range:NSMakeRange(length, 4)];
        length += 4;
        NSString *content = [SocketMethod stringSubdata:data Range:NSMakeRange(length, contentLenth)];
        length += contentLenth;
        NSArray *array = [content componentsSeparatedByString:@"|"];
        for (NSString *string in array) {
            if (string.length > 0) {
                NSArray *jsonArray = [string componentsSeparatedByString:@","];
                NSString *messageType = [SocketMethod subArray:jsonArray Index:0];
                int count = [[SocketMethod subArray:jsonArray Index:1] intValue];
                if ([messageType isEqualToString:@"00"]) {
                    self.systemMessageCount = count;
                } else if ([messageType isEqualToString:@"01"]) {
                    self.businessMessageCount = count;
                } else if ([messageType isEqualToString:@"02"]) {
                    self.personalMessageCount = count;
                } else if ([messageType isEqualToString:@"04"]) {
                    self.receiveMessageCount = count;
                }
            }
        }
        self.noReadCount = self.systemMessageCount + self.businessMessageCount + self.personalMessageCount + self.receiveMessageCount;
        self.waitExpressesCount = [[SocketMethod stringSubdata:data Range:NSMakeRange(length, 16)] intValue];
        length += 16;
        self.rejectUserCount = [[SocketMethod stringSubdata:data Range:NSMakeRange(length, 16)] intValue];
        if ([Singleton shareInstance].userMessage) {
            NSString *lastTime = [[NSUserDefaults standardUserDefaults] objectForKey:[Singleton shareInstance].userMessage.userId];
            if (![self.messageNewTime isEqualToString:lastTime] && self.noReadCount > 0) {
                self.isNewMesasge = YES;
            }
        }
    }
    return self;
}

@end
